package com.cskaoyan.controller;

import com.cskaoyan.module.bean.BaseRespVo;
import com.cskaoyan.module.bean.GoodsData;

import com.cskaoyan.module.bo.goods.CreateGoodsBo;
import com.cskaoyan.module.bo.goods.UpdateGoodsBo;
import com.cskaoyan.module.pojo.MarketGoods;
import com.cskaoyan.module.vo.goods.*;
import com.cskaoyan.module.bean.param.BaseParam;

import com.cskaoyan.service.GoodsService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

/**
 * 商品管理
 * 创建日期: 2022/04/06 19:53
 *
 * @author hxing
 */
@RestController
@RequestMapping("admin/goods")
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    @GetMapping("list")
    public BaseRespVo list(SeekGoodsList seekGoodsList, BaseParam baseParam){

        GoodsListLimitVo result=new GoodsListLimitVo();

        if (baseParam.getLimit() == 0) {
            result=goodsService.listLimit0(0);
            return BaseRespVo.ok(result);
        }
        GoodsData goodsData = goodsService.query(seekGoodsList, baseParam);


        return BaseRespVo.ok(goodsData);
    }

    @GetMapping("catAndBrand")
    public BaseRespVo catAndBrand(){

        CartAndBrandVo catAndBrandVo = goodsService.seekCatAndBrand();

        return BaseRespVo.ok(catAndBrandVo);

    }
    @GetMapping("detail")
    public BaseRespVo detail(Integer id){
      GoodsDetailVo goodsDetailVo = goodsService.seekDetailById(id);
        BaseRespVo<Object> vo = new BaseRespVo<>();
        vo.setErrmsg("成功");
        vo.setErrno(0);
        return vo.ok(goodsDetailVo);
    }
    @PostMapping("create")
    public BaseRespVo create(@RequestBody CreateGoodsBo createGoodsBo){
        BaseRespVo vo = goodsService.createGoods(createGoodsBo);
        return vo ;
    }

    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody MarketGoods marketGoods){

        goodsService.deleteGoods(marketGoods);

        return BaseRespVo.ok("成功");
    }
    @PostMapping("update")
    public BaseRespVo update(@RequestBody UpdateGoodsBo updateGoodsBo){

        goodsService.updateGoods(updateGoodsBo);

        return BaseRespVo.ok("成功") ;
    }
}
